<?php
/**
 * 控制器基础类，所有控制器均需继承此类
 * @author chenfenghua <843958575@qq.com>
 * version v2.0
 */
namespace app\backend\components;

use Yii;
use yii\web\Controller;

class BaseController extends Controller
{
    public $layout = '@app/backend/views/layouts/main';
    public $pageSize = 15;
    public $data = ['js'=>[], 'css'=>[], 'submit'=>0, 'action_list'=>'index'];
    const  CODE_ERR = '400';
    const  CODE_SUC = '200';
    public function init()
    {
        parent::init(); // TODO: Change the autogenerated stub
        $this->data['module_id'] = $this->module->id;

        $this->data['controller_id'] = $this->id;
        $this->data['submit'] = Yii::$app->request->get('ref_sub', '0');

        #屏蔽登录、注销等基本用户操作
        if (!($this->module->id == 'admin' && $this->id == 'user')) {
            $this->checkLogin();
            $this->checkPermission();
        }
    }

    /**
     * 判断用户是否登录
     */
    public function checkLogin()
    {
        if (!Yii::$app->session['admin_user_id']) $this->redirect('?r=admin/user/login');
        return true;
    }

    /**
     * 验证权限
     *
     * @return bool
     */
    public function checkPermission()
    {
        return true;
        $route = Yii::$app->request->get('r');
        if (Yii::$app->session['super'] == 1) return true;
        if (in_array($route, Yii::$app->params['exceptPermission'])) return true;
        if (strpos(Yii::$app->session['acl'], $route) === false) $this->redirect('?r=admin/default/permission');
        return true;
    }

    /**
     * JSON返回成功
     *
     * @param string $msg
     * @param array $data
     * @return array
     */
    public function jsonSuccess($msg = '', $data = [])
    {
        return ['code'=>200, 'msg'=>$msg, 'data'=>$data];
    }

    /**
     * JSON返回失败
     *
     * @param string $msg
     * @param array $data
     * @return array
     */
    public function jsonFail($msg = '', $data = [])
    {
        return ['code'=>500, 'msg'=>$msg, 'data'=>$data];
    }

}